<template>
  <div class="boxs">
    <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
    >
    
    <el-form-item label="状态" prop="qsftg" >
          <el-select v-model="queryParams.qsftg" placeholder="请选择审批结果" >
            <el-option  label="未处理" :value="0">
            </el-option>
            <el-option  label="通过" :value="1">
            </el-option>
            <el-option  label="不通过" :value="2">
            </el-option>
          </el-select>
        </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
        >搜索</el-button>
      </el-form-item>

      <el-button icon="el-icon-plus" type="primary" plain size="small" @click="openDialog">添加报警信息</el-button>

    </el-form>
    <el-table :data="alarmArr" style="width: 100%" v-loading="loading">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="报警编号" align="center" prop="number" />
      <el-table-column label="英文描述" align="center" prop="desc_en"/>
      <el-table-column label="中文描述" align="center" prop="desc_ch"/>
      <el-table-column label="创建时间" align="center" prop="create_time"  />


      <el-table-column label="操作" align="center" width="350">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.admin!==1" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>

          <el-button size="mini" v-if="scope.row.admin!==1" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>

        </template>
      </el-table-column>      

    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.size"
        @pagination="getAlarmList"
    />



    <el-dialog title="操作框" :visible.sync="dialogVisible" width="40%">
      <el-form class="demo-form-inline" label-width="80px" :model="form" >
        
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"  />
        </el-form-item>
        
        <el-form-item label="报警编号" prop="number">
          <el-input v-model="form.number"  />
        </el-form-item>

        <el-form-item label="英文描述" prop="desc_en" >
          <el-input  v-model="form.desc_en">
          </el-input>
        </el-form-item>
        <el-form-item label="中文描述" prop="desc_ch" >
          <el-input  v-model="form.desc_ch">
          </el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" v-loading="addLoading">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :type="form.id ? 'warning' : 'primary'" @click="affirm">{{
            form.id ? "确认修改" : "确认添加"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getAlarmList, addAlarm, upAlarm, delAlarm} from '@/api/admin';
import {formatDate} from "@/utils";
import md5 from 'js-md5';
export default {
  name:"Alarm",
  data () {
    return {
      queryParams:{
        page:1,
        size:10
      },
      total:0,
      loading:false,
      addLoading:false,
      alarmArr: [],
      dialogVisible: false,
      themeOpen:false,
      rolesArr: [],
      moreArr: [],
      search: '',
      form: {
      },
    }
  },
  async created () {
    this.getAlarmList()
  },
  methods: {
    formatterCreateTime(row){
      return formatDate(row.createTime);
    },
    formatMore (row) {
      if(row.admin===1) return "总管理";
      let res = this.moreArr.filter(t => t.id == row.moreId);
      if (res.length == 0) return "“账号不存在”"
      return res[0].name
    },
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该报警, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        await delAlarm({ id: row.id });
        this.getAlarmList();
        this.$message({
          message: '删除成功！',
          type: 'success'
        });
      })
    },
    resetForm () {
      this.form = {status:1};
    },
    handleEdit (index, row) {
      this.resetForm();
      this.form = {...row};
      this.dialogVisible = true;
    },
    async affirm () {
      try {
        this.addLoading=true;
        console.log(this.form);

        !this.form.id && await addAlarm({...this.form});
        this.form.id && await upAlarm({ ...this.form});
        this.addLoading=false;
        this.$message.success(this.form.id?'修改成功！':'添加成功！');
        this.getAlarmList();
        this.dialogVisible = false;
      }catch (e) {
        this.addLoading=false;
      } 
    },
    openDialog () {
      this.resetForm();
      this.dialogVisible = true
    },

    async getAlarmList () {
      this.loading=true;
      const {data,total} = await getAlarmList(this.queryParams);
      this.loading=false;
      this.alarmArr = data;this.total=total;
    },
    handleQuery(){
      this.queryParams.page=1;
      this.getAlarmList(this.queryParams);
    }
  }
}
</script>
<style scoped lang="scss">
.boxs {
  padding: 20px;
}
.color-title{
  margin-bottom: 10px;
  color: #000;
}
</style>
